<template>
	<view class="comments">
		<view class="wrap">
			<view class="addtp">
				<view v-if="imageLists.length !=0" class="updated" v-for="(s,i) in imageLists" :key="i">
					<image class="img" :src="s" mode="" @tap="previewImages"></image>
				</view>
				<view class="pushPic" @click="selects" v-if="imageLists.length==0">
					<uni-icons type="camera-filled" size="30"></uni-icons>
					<view class="txt">添加图片</view>
				</view>
			</view>
		</view>

		<view class="other">
			<view class="commtext">
				<span>外观描述</span>
				<span>商品品质</span>
				<span>满意程度</span>
			</view>
			<textarea class="textas" @input="getValue" cols="30" rows="10" placeholder="请输入您宝贵的评价"></textarea>

			<view class="btn">
				<button @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				bh: '',
				imageLists: [],
				pic:'',
				value: ''
			}
		},
		onLoad(options) {
			this.bh = options.bh
			console.log(this.bh)
		},
		onShow() {

			var bs = uni.getStorageSync('UserInformation')
			this.token = bs.usertoken
		},
		methods: {
			previewImages: function(e) {
				var current = e.target.dataset.src;
				uni.previewImage({
					current: current,
					urls: this.imageList
				});
			},
			selects() {
				this.chooseImagess();
			},
			chooseImagess() {
				var th = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: res => {

						this.arr = res.tempFilePaths
						for (var i = 0; i < this.arr.length; i++) {

							uni.uploadFile({
								url: 'http://localhost:9999/mall/upload/uploadImage',
								filePath: this.arr[i],
								name: 'file',
								header: {
									"content-type": "multipart/form-data"
								},
								success: function(res) {
									var data = JSON.parse(res.data)
									
									th.imageLists.push(data.data)
									th.pic = data.data
								},

								fail(res) {
									console.log('失败res', res)
								}
							})
						}
					}
				})
			},
			getValue(e) {
				this.value = e.detail.value
			},
			submit() {
				console.log(this.value,this.imageLists,this.pic)
				this.addComment()
			},
			addComment() {
				uni.showLoading({
					title: '加载中'
				});

				var th = this
				this.requestfs({
					url: '/mall/comment/addComment',
					data: {
						commentms: th.value,
						commenttpbh: th.pic,
						goodbh: th.bh
					},
					method: 'POST',
					Authorization: this.token,
				}).then(res => {
					console.log('res:', res)
					if (res.data.code == 200) {
						uni.hideLoading()
						uni.switchTab({
							url:'/pages/share/share'
						})
					} else {
						uni.showToast({
							title: "提交评论失败",
							icon: "none",
							duration: 2000
						})
					}
				})
			},

		}
	}
</script>

<style lang="scss">
	.comments {
		width: 100%;
		background-image: linear-gradient(to right, rgba(242, 242, 254, 1), rgba(248, 226, 228, 1));
		height: 100vh;
		padding-top: 20rpx;

		.other {
			margin-top: 20rpx;
			width: 100%;
			height: 80vh;
			border-radius: 20rpx;
			background-color: #fff;

			.commtext {
				width: 90%;
				margin: 0 auto;

				span {
					display: inline-block;
					font-size: 26rpx;
					border: 1px dashed #d1d1d1;
					border-radius: 50rpx;
					padding: 8rpx 20rpx;
					margin: 20rpx 20rpx 20rpx 0;

				}
			}

			.textas {
				width: 90%;
				margin: 0 auto;
				font-size: 28rpx;
			}

			.btn {
				width: 100%;
				margin-top: 200rpx;

				button {
					width: 75%;
					margin: 0 auto;
					background-image: linear-gradient(to right, rgba(237, 127, 48, 0.7), rgba(236, 96, 45, 1));
					height: 80rpx;
					line-height: 80rpx;
					color: #ffffff;
					border-radius: 50rpx;
				}
			}

		}


		.wrap {
			width: 90%;
			margin: 0 auto;

			.addtp {
				width: 100%;
				height: 240rpx;
				border: 2px solid #fff;
				border-radius: 20rpx;
				background-color: #fff;

				.pushPic {
					color: black;
					width: 220rpx;
					height: 110rpx;
					text-align: center;
					border-radius: 10rpx;
					margin: 65rpx auto;
				}

				.updated {
					margin: 10rpx auto;
					width: 220rpx;
					height: 220rpx;

					.img {
						width: 220rpx;
						height: 220rpx;
					}
				}
			}
		}

	}
</style>
